package com.awenhui.demo.core.utils.zutils;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;

/**
 * 时间处理工具类
 *
 * @author zhouxuanhong
 * @date 2019-04-01 16:55
 * @since JDK1.8
 */
public class KtwlDateUtils {
    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String YYYYMMDDHH = "yyyyMMddHH";
    public static final String YYYYMMDDHHMM = "yyyyMMddHHmm";
    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static final String YYYYMMDDHHMMSSSS = "yyyyMMddHHmmssSS";
    public static final String FORMAT_YYYY_MM_DD = "YYYY-MM-dd";
    public static final String FORMAT_YYYY_MM_DD_HH = "YYYY-MM-dd HH";
    public static final String FORMAT_YYYY_MM_DD_HH_MM = "YYYY-MM-dd HH:mm";
    public static final String FORMAT_YYYY_MM_DD_HH_MM_SS = "YYYY-MM-dd HH:mm:ss";
    public static final String FORMAT_YYYY_MM_DD_HH_MM_SS_SS = "YYYY-MM-dd HH:mm:ss:SS";
    private static SimpleDateFormat sdf;


    public static String getStringDate(String format) {
        Date date = new Date();
        return formatDate(date, format);
    }

    public static String getStringDate(String format, Date date) {
        return formatDate(date, format);
    }

    public static String getStringDate(Date date) {
        return formatDate(date, FORMAT_YYYY_MM_DD_HH_MM_SS);
    }


    public static String formatDate(Date date, String format) {
        if (date == null) {
            return "";
        }
        if (StringUtils.isBlank(format)) {
            return "";
        }
        sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * 根据 localDate 格式化时间
     * @param localDate 日期
     * @param format  格式
     * @return 格式化日期字符串
     */
    public static String formatDate(LocalDate localDate, String format) {
        if (localDate == null || StringUtils.isBlank(format)) {
            return "";
        }
        long milli = localDate.atStartOfDay().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        Date date = new Date(milli);
        sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }
    /**
     * 根据 LocalDateTime 格式化时间
     * @param localDateTime 日期
     * @param format  格式
     * @return 格式化日期字符串
     */
    public static String formatDate(LocalDateTime localDateTime, String format) {
        if (localDateTime == null || StringUtils.isBlank(format)) {
            return "";
        }
        long milli = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        Date date = new Date(milli);
        sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static String formatDate(String strDate, String format,boolean type) {
        if (strDate == null || StringUtils.isBlank(format) ) {
            return "";
        }
        Long time = null;
        if(strDate.length() > 10){
            try {
                time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(strDate).getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else{
            try {
                time = new SimpleDateFormat("yyyy-MM-dd").parse(strDate).getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        sdf =  new SimpleDateFormat(format);
        return sdf.format(time);
    }

    public static Date formatDate(String strDate, String format) {
        if (StringUtils.isBlank(strDate)) {
            return null;
        }
        sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static Date formatDate(String strDate) {
        return formatDate(strDate, FORMAT_YYYY_MM_DD_HH_MM_SS);
    }

    public static Long formatLong(Date date) {
        return date.getTime();
    }

    public static Long formatLong(String source, String format) {
        if (StringUtils.isBlank(source)) {
            return null;
        }
        sdf = new SimpleDateFormat(format);
        try {
            Date date = sdf.parse(source);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取精确到秒的时间戳
     *
     * @return
     */
    public static int getSecondTimestampTwo() {
        Date date = new Date();
        if (null == date) {
            return 0;
        }
        String timestamp = String.valueOf(date.getTime() / 1000);
        return Integer.valueOf(timestamp);
    }
}
